import { useState } from "react";
import reactLogo from "@assets/react.svg";
import viteLogo from "/vite.svg";
import { userStore } from "../../store/home";
import { useLocalObservable, useObserver, Observer } from "mobx-react-lite";

import Index from "@pages/index/";
function Home() {
  const [count, setCount] = useState(0);

  let [props, setProps] = useState("");

  const showMianProps = () => {
    props = window.$wujie.props;
    setProps(JSON.stringify(props));
  };

  // 发送事件给父应用
  const senMsg = () => {
    window.$wujie?.bus.$emit("onSubAppProps", "react", 18);
  };
  // 接收父应用事件
  window.$wujie?.bus.$on("onReport", function (name: string, age: number) {
    console.log("我是react 监听来自父应用事件", name, age);
  });

  return (
    <>
      <Index></Index>
      <div>
        <a href="https://vitejs.dev" target="_blank">
          <img src={viteLogo} className="logo" alt="Vite logo" />
        </a>
        <a href="https://react.dev" target="_blank">
          <img src={reactLogo} className="logo react" alt="React logo" />
        </a>
      </div>
      <h1>Vite + React</h1>
      {/* mobx监听组件，类型flutter的provider，哪里需要哪里包 */}
      <Observer>
        {() => (
          <div
            onClick={() =>
              userStore.changeRoleType(Math.floor(Math.random() * 100))
            }
          >
            点击改变索引{userStore.roleName}
          </div>
        )}
      </Observer>
      <button onClick={showMianProps}>点我打印主应用参数: {props}</button>
      <button onClick={senMsg}>点我向主应用传参</button>
      <div className="card">
        <button onClick={() => setCount((count) => count + 1)}>
          count is {count}
        </button>
        <p>
          Edit <code>src/App.tsx</code> and save to test HMR
        </p>
      </div>
      <p className="read-the-docs">
        Click on the Vite and React logos to learn more
      </p>
    </>
  );
}

export default Home;
